<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Teacher
 *
 * @author Rodrigo
 */
class Teacher {

    private $id;
    private $workLoadRgt;
    private $balance;
    private $workLoadLft;
    private $disciplines;
    private $disciplineWeights;
    private $situation;

    public function __construct($id, $workLoad, $situation) {
        $this->setId($id);
        $this->setSituation($situation);
        $this->setWorkLoadRgt($workLoad);
        $this->setWorkLoadLft($workLoad);
        $this->disciplines = array();
        $this->balance = 0;
        $this->getWeights();
        
    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getWorkLoadRgt() {
        return $this->workLoadRgt;
    }

    public function setWorkLoadRgt($workLoadRgt) {
        if($this->situation[1] && $this->situation[2])
            $this->workLoadRgt = $workLoadRgt;
        elseif($this->situation[1] && !$this->situation[2])
            $this->workLoadRgt = 2*$workLoadRgt;
        else
            $this->workLoadRgt = 0;
    }

    public function setSituation($situation) {
        foreach ($situation as $key => $semester) {
            $this->situation[$key] = ($semester == 1)?true:false;
        }
    }

    public function getSituation($semester) {
        return $this->situation[$semester];
    }

    public function getBalance() {
        return $this->balance;
    }

    public function setBalance($balance) {
        $this->balance = $balance;
    }

    public function getWorkLoadLft() {
        return $this->workLoadLft;
    }

    public function setWorkLoadLft($workLoadLft) {
        if($this->situation[1] && $this->situation[2])
             $this->workLoadLft = $workLoadLft;
        elseif(!$this->situation[1] && $this->situation[2])
             $this->workLoadLft = 2*$workLoadLft;
        else
            $this->workLoadLft = 0;
    }

    public function addDiscipline(Discipline $discipline) {
        if ($this->isAble($discipline)) {
            $this->disciplines[] = $discipline;

            if ($discipline->getSemester() == 1) {
                $this->workLoadRgt -= $discipline->getWorkLoad();
                if($this->situation[2] && $this->situation[1]) $this->balance += 1;
            } elseif ($discipline->getSemester() == 2) {
                $this->workLoadLft -= $discipline->getWorkLoad();
                if($this->situation[2] && $this->situation[1]) $this->balance -= 1;
            }
            return true;
        }
        return false;
    }

    private function isAble($discipline) {
        if($this->situation[$discipline->getSemester()])
            if ($discipline->getSemester() == 1)
                return ($this->workLoadRgt >= $discipline->getWorkLoad()) ? true : false;
            elseif ($discipline->getSemester() == 2)
                return ($this->workLoadLft >= $discipline->getWorkLoad()) ? true : false;
    }

    public function getDiscipline($index) {
        return $this->disciplines[$index];
    }

    public function getDisciplines() {
        return $this->disciplines;
    }

    public function searchDiscipline($id) {
        for ($i = 0; $i < sizeof($this->disciplines) && $this->disciplines[$i]->getId() != $id; $i++)
            ;

        return ($i == sizeof($this->disciplines)) ? -1 : $i;
    }

    public function isBalanced($semester) {
        if(!$this->situation[$semester]) return false;

        if ($semester == 1)
            return ($this->balance <= 0) ? true : false;
        elseif ($semester == 2)
            return ($this->balance >= 0) ? true : false;
    }

    private function getWeights() {
        $weightsM = new Weights();

        $weights = $weightsM->find(array('condition' => array('_teacher_id' => $this->id), 'order' => array('_disciplines_id'), 'orderCondition' => 'ASC'));

        foreach ($weights as $weight):
            $this->disciplineWeights[$weight['_teacher_disciplines']['_disciplines_id']] = $weight['_teacher_disciplines']['_weight'];
        endforeach;
    }

    public function getWeight($discipline) {
        return ($this->disciplineWeights === null || !isset($this->disciplineWeights[$discipline]))?null:$this->disciplineWeights[$discipline];
    }

}

?>
